#!/usr/bin/python3
import json
import os
import sys
from prettytable import PrettyTable

class ServerBean(object):
	"""docstring for ClassName"""
	inIp="";
	outIp="";
	password="";
	remark="";
	user="";
	
	def __init__(self,inIp,outIp,password,remark,user):
		self.inIp = inIp;
		self.outIp = outIp;
		self.password = password;
		self.remark = remark;
		self.user = user;

numServerList=[];
numEnvList=[];

# 菜单类
class Menu(object):

	# 展示环境列表
	def showEnvList(self,serverConfigPath):
		envNum = 1;
		# 每次清空列表缓存
		numEnvList.clear();
		with open(serverConfigPath,'r') as load_f:
			serverJson = json.load(load_f);
			for envList in serverJson:
				env = envList["ENV"];
				print(str(envNum)+":"+env)
				envNum = envNum+1;
				# 初始化环境列表缓存
				numEnvList.append(env);

	def getEnvByNum(self,num):
		return numEnvList[num];

	# 展示服务器列表
	def showServerList(self,env,serverConfigPath):
		serverNum = 1;
		# 每次清空列表缓存
		numServerList.clear();
		with open(serverConfigPath,'r') as load_f:
			serverJson = json.load(load_f);
			for envList in serverJson:
				if(envList["ENV"] == env):
					print("+========================"+envList["ENV"]+"===============================+")
					table=PrettyTable(["编号","内网ip","外网ip","备注"])
					serviceList = envList["SERVICE_LIST"];
					for service in serviceList:
						table.add_row([serverNum,service["inIp"],service["outIp"],service["remark"]])

						sBean=ServerBean(service["inIp"],service["outIp"],service["password"],service["remark"],service["user"]);
						# 初始化服务器列表缓存
						numServerList.append(sBean);
						serverNum=serverNum+1;

					table.align["内网ip"] = 'l';
					table.align["外网ip"] = 'l';
					table.align["备注"] = 'l';
					print(table);

# 用户操作类
class UserAction(object):
	menu = Menu();
	# 当前用户选择的环境
	currentEnv = "";
	# 当前用户步骤
	currentStep = 0;
	# 是否还未登陆，当用户登陆ssh后,再退出登陆时，会更新此状态。防止再次登陆
	notLogin=True;
	# 输入校验
	def intputCheck(self,message,step):
		num = input(message+":")
		if num == "":
			sys.exit(0);
		return num;
	# 输入处理方法
	def inputHandler(self,num,maxInputLength):
		if(int(num) == 0):
			if(self.currentStep == 1):
				sys.exit(0);
			if(self.currentStep == 2):
				self.envChoose();

		if(int(num) > maxInputLength):
			if(self.currentStep == 1):
				self.envChoose();
			if(self.currentStep == 2):
				self.serverChoose();


	# 选择环境
	def envChoose(self):
		# 菜单类，展示列表
		self.menu.showEnvList(serverConfigPath);
		self.currentStep = 1;
		# 环境长度
		numEnvLength = len(numEnvList);

		num = userAction.intputCheck("please input env num",self.currentStep);
		# 处理用户输入
		self.inputHandler(num,numEnvLength);
		self.currentEnv = self.menu.getEnvByNum(int(num)-1);
		self.serverChoose();

	# 选择服务器
	def serverChoose(self):
		self.menu.showServerList(self.currentEnv,serverConfigPath);
		self.currentStep = 2;
		num = userAction.intputCheck("please input server num",self.currentStep);
		# 处理用户输入
		self.inputHandler(num,len(numServerList));
		if(self.notLogin):
			server = numServerList[int(num)-1];
			os.system("./conn_server.sh "+server.user+" "+server.password+" "+server.inIp)
			# 设置为登陆状态
			self.notLogin = False;
		else:
			# 设置为未登陆状态
			self.notLogin = True;
			self.serverChoose();

serverConfigPath="serverConfig.json";


# 输入校验类
userAction=UserAction();
userAction.envChoose();



		
